package ink.xiaobaibai.common.delay;

import lombok.Data;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @description: 机器延时消息
 * @author: 小白白
 * @create: 2021-05-31
 **/

@Data
public class DeviceDelayItem implements Delayed {

    private Integer deviceId;
    private Date expiredTime;

    public DeviceDelayItem(Integer deviceId, Date expiredTime) {
        this.deviceId = deviceId;
        this.expiredTime = expiredTime;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expiredTime.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return this.expiredTime.compareTo(((DeviceDelayItem) o).expiredTime);
    }

    @Override
    public int hashCode() {
        return this.deviceId;
    }

    @Override
    public boolean equals(Object obj) {
        return this.deviceId.equals(((DeviceDelayItem) obj).deviceId);
    }

}
